package com.hycreate.platform.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.sys.entity.Dict;
import com.hycreate.platform.modules.sys.service.DictService;
import com.hycreate.platform.modules.sys.utils.DictUtils;

/**
 * 字典Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/dict")
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public Dict get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return dictService.get(id);
		}else{
			return new Dict();
		}
	}
	
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = {"list", ""})
	public String list(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<String> typeList = dictService.findTypeList();
		model.addAttribute("typeList", typeList);
        Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict); 
        model.addAttribute("page", page);
		return "modules/sys/dictList";
	}

	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "form")
	public String form(Dict dict, Model model) {
		//处理添加键值时乱码
		if(dict != null && StringUtils.isNotBlank(dict.getDescription())){
			List<Dict> dicts = DictUtils.getDictList(dict.getType());
			dict.setDescription(dicts.get(0).getDescription());
		}
		
		model.addAttribute("dict", dict);
		return "modules/sys/dictForm";
	}

	@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "save")//@Valid 
	public String save(Dict dict, Model model, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/dict/?repage&type="+dict.getType();
		}
		if (!beanValidator(model, dict)){
			return form(dict, model);
		}
		dictService.save(dict);
		addMessage(redirectAttributes, "保存字典'" + dict.getLabel() + "'成功");
		return "redirect:" + adminPath + "/sys/dict/?repage&type="+dict.getType();
	}
	
	@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "delete")
	public String delete(Dict dict, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/dict/?repage";
		}
		dictService.delete(dict);
		addMessage(redirectAttributes, "删除字典成功");
		return "redirect:" + adminPath + "/sys/dict/?repage&type="+dict.getType();
	}
	
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String type, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Dict dict = new Dict();
		dict.setType(type);
		List<Dict> list = dictService.findList(dict);
		for (int i=0; i<list.size(); i++){
			Dict e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentId());
			map.put("name", StringUtils.replace(e.getLabel(), " ", ""));
			mapList.add(map);
		}
		return mapList;
	}
	
	@ResponseBody
	@RequestMapping(value = "listData")
	public List<Dict> listData(@RequestParam(required=false) String type) {
		Dict dict = new Dict();
		dict.setType(type);
		return dictService.findList(dict);
	}
	
  /**
   * 学校管理
   */
	/*@RequiresPermissions("sys:dict:view")
	@RequestMapping(value ="schoollist")
	public String schoollist(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		dict.setType("sys_graduated_school");
		List<String> typeList = dictService.findTypeList();
		model.addAttribute("typeList", typeList);
        Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict); 
        model.addAttribute("page", page);
		return "modules/school/schoolList";
	}*/
	
	/**
	 * 学校添加页面跳转
	 * @param dict
	 * @param model
	 * @return
	 */
	/*@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "schoolform")
	public String schoolform(Dict dict, Model model) {
		//处理添加键值时乱码
		if(dict != null && StringUtils.isNotBlank(dict.getDescription())){
			List<Dict> dicts = DictUtils.getDictList(dict.getType());
			dict.setDescription(dicts.get(0).getDescription());
		}
		
		model.addAttribute("dict", dict);
		return "modules/school/schoolFrom";
	}
	*/
	/**
	 * 学校保存页面跳转
	 * @param dict
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	/*@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "schoolsave")//@Valid 
	public String schoolsave(Dict dict, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/dict/schoollist?repage&type="+dict.getType();
		}
		if (!beanValidator(model, dict)){
			return form(dict, model);
		}
		List<Dict> list = dictService.findType(dict);//获取后台字典值
		String i= list.get(list.size()-1).getValue();
		int o=Integer.valueOf(i);
		String value=o+1+"";
		dict.setValue(value);
		int s= list.get(list.size()-1).getSort();//获取排序值
		dict.setSort(s+10);
		dict.setDescription(list.get(list.size()-1).getDescription());
		dictService.save(dict);
		addMessage(redirectAttributes, "保存字典'" + dict.getLabel() + "'成功");
		return "redirect:" + adminPath + "/sys/dict/schoollist?repage&type="+dict.getType();
	}*/
	
	/**
	 * 学校删除
	 * @param dict
	 * @param redirectAttributes
	 * @return
	 */
	/*@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "schooldelete")
	public String schooldelete(Dict dict, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/dict/schoollist?repage";
		}
		dictService.delete(dict);
		addMessage(redirectAttributes, "删除字典成功");
		return "redirect:" + adminPath + "/sys/dict/schoollist?repage&type="+dict.getType();
	}*/
	/**
	 * 专业管理列表页面跳转
	 * @param dict
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	/*@RequiresPermissions("sys:dict:view")
	@RequestMapping(value ="professionlist")
	public String professionlist(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		dict.setType("sys_major");
		List<String> typeList = dictService.findTypeList();
		model.addAttribute("typeList", typeList);
        Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict); 
        model.addAttribute("page", page);
		return "modules/profession/professionList";
		
	}*/
	
	/**
	 * 专业添加页面跳转
	 * @param dict
	 * @param model
	 * @return
	 */
	/*@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "professionform")
	public String professionform(Dict dict, Model model) {
		//处理添加键值时乱码
		if(dict != null && StringUtils.isNotBlank(dict.getDescription())){
			List<Dict> dicts = DictUtils.getDictList(dict.getType());
			dict.setDescription(dicts.get(0).getDescription());
		}
		
		model.addAttribute("dict", dict);
		return "modules/profession/professionFrom";
	}*/
	
	/**
	 * 专业保存页面跳转
	 * @param dict
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	/*@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "professionsave")*///@Valid 
	/*public String professionsave(Dict dict, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/dict/professionlist?repage&type="+dict.getType();
		}
		if (!beanValidator(model, dict)){
			return form(dict, model);
		}
		List<Dict> list = dictService.findType(dict);//获取后台字典值，根据sort值进行排序
		String i= list.get(list.size()-1).getValue();//获取最大键值
		int o=Integer.valueOf(i);
		String value=o+1+"";
		dict.setValue(value);
		int s= list.get(list.size()-1).getSort();//获取排序值
		dict.setSort(s+10);
		dict.setDescription(list.get(list.size()-1).getDescription());
		dictService.save(dict);
		addMessage(redirectAttributes, "保存字典'" + dict.getLabel() + "'成功");
		return "redirect:" + adminPath + "/sys/dict/professionlist?repage&type="+dict.getType();
	}*/
	/**
	 * 专业删除
	 * @param dict
	 * @param redirectAttributes
	 * @return
	 */
/*	@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "professiondelete")
	public String professiondelete(Dict dict, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/dict/professionlist?repage";
		}
		dictService.delete(dict);
		addMessage(redirectAttributes, "删除字典成功");
		return "redirect:" + adminPath + "/sys/dict/professionlist?repage&type="+dict.getType();
	}*/
}
